<?php
	require_once 'includes/image_resizer.php';
?>
<div style=" display: none; width: 400px; font-weight:bold; padding:10px; background-color:#cccccc; opacity:0.9;" class="user_popup" id="more_infow1">
  <span style="float:left; width:100%; text-align:right; margin-bottom:5px;"><label style="cursor:pointer;" onclick="$('#more_infow1').fadeOut('slow');">x</label></span>
  <span style="float:left; width:100%; margin-bottom:10px;" id="desc_info">Wanted Items are here to keep track of what you want to get in the future. Plus Sellers will make offers towards your specific wants and price to benefit you. </span>
</div>

<script>
function show_more_infow()
{
  var ntop = ( $(window).height() - $('#more_infow1').height() ) / 2+$(window).scrollTop();
  var nleft = ( $(window).width() - $('#more_infow1').width() ) / 2+$(window).scrollLeft();

  $('#more_infow1').css({'left': nleft, 'top':ntop});
  $('#more_infow1').fadeIn('slow');
}
</script>

<a name="wishlist" id="wishlist"></a>
<div id='panel'>
<div class="fulldiv" style="width:700px;">

 <div class="fulldiv" style="margin:10px 0px 0px 0px;">
	
 	<div style="float:left; margin:0px 10px 0px 10px; padding-top:8px;"><strong class="orangehead">My Wanted List</strong></div>
	<div class="greyarrowsml" id="arrow_panel_wishlist" onclick="show_hide_s_boxes('sell_u_p')">&nbsp;</div>
	
	<div class="fulldiv" style="width:49%; margin-top:13px; float:right;">
	<div class="star" style="float: right;" id="star_panel_wishlist">&nbsp;</div>
	<a href="javascript:void(0)" class="bluelink" style="float: right;" onclick="show_more_infow()" id="more_info_panel_wishlist">more info</a>
	</div>

<div id="sell_u_p">
<div class="fullbox_top">&nbsp;</div>
	<div class="fullbox_repeat">
<table width="100%" align="center" border="0" cellpadding="0" cellspacing="0">
 <tr>
  <td>	
 <?php
	if(!empty($_POST['hidDelWishlist'])) {	
		$wId=$_POST['hidDelWishlist'];
		//get wishlist old images
		$sqlGetImage = "SELECT product_image, product_thumb FROM tbl_wishlist WHERE wishlist_id='$wId'";
		$rstGetImage = mysql_query($sqlGetImage) or die(mysql_error());
		$rowGetImage = mysql_fetch_array($rstGetImage);
		
		$sqlDelPro="Delete from tbl_wishlist where wishlist_id=$wId";
		$rstDelPro=mysql_query($sqlDelPro);
		if($rstDelPro) { 			
			//delete old images
			if($rowGetImage[0]!="") { unlink($rowGetImage[0]); }
			if($rowGetImage[1]!="") { unlink($rowGetImage[1]); } 
			
			echo "<div id='suc-msg' style='width:650px;' align='center'> Item deleted from your wishlist. </div>";			
		}	
		else {
			echo "<div id='err-msg' style='width:650px;' align='center'> Error on deleting wishlist item. </div>";
		}
	}
	
	
	if(!empty($_POST['hidOrderWishlist'])) {
		$sqlSelLstOrder="SELECT * FROM tbl_wishlist where user_id='$userId'";
		$rstSelLstOrder=mysql_query($sqlSelLstOrder);
		$i=0;
		while($rowSelLstOrder=mysql_fetch_array($rstSelLstOrder))
		{
		$wishId=$rowSelLstOrder['wishlist_id'];
		$wishOrder=$_POST['wishOrder_'.$wishId.''];
		if(!empty($wishOrder) && !is_nan($wishOrder))
		{
		$sqlUpdWishList="UPDATE tbl_wishlist SET product_order=".$wishOrder." WHERE wishlist_id=$wishId";
		$rstUpdWishList=mysql_query($sqlUpdWishList);
		}
		$i++;
		}
		if($i>0){ 			
			echo "<div id='suc-msg'> Your wishlist order changed. </div>"; 
		}
		else {
			echo "<div id='err-msg'> Error on changing wishlist order. </div>";
		}
	}
	
	
	if(!empty($_POST['addWishListKey'])) {
		$productName=$_POST['productName'];
		$wishprice=$_POST['wishprice'];
		$productDescription=$_POST['productDescription'];
		$currentDate=date("Y-m-d");
		$sqlAddWishList="INSERT INTO tbl_wishlist set user_id='$userId', wishlist_type=2, product_name='$productName', price='$wishprice', description='$productDescription',status=1, created_date='$currentDate'";
		$rstAddWishList=mysql_query($sqlAddWishList) or die(mysql_error());
		if(mysql_affected_rows()>0) {	
			$wishId=mysql_insert_id();
			$wishImage = $_FILES["productImage"]["name"];
			$imageInfo=pathinfo($wishImage);		
			$wishType = strtolower($imageInfo['extension']);
			if(!empty($wishType)) {						
				if($wishType=="jpeg" || $wishType=="jpg" || $wishType=="png" || $wishType=="gif") {				
					$rand=mt_rand();					
					$path="wishlist/source/".$rand.$wishId.".".$wishType;					
					$path_thumb="wishlist/thumb/".$rand.$wishId."_thumb.".$wishType;					
					$path_medium="wishlist/medium/".$rand.$wishId."_medium.".$wishType;									
					move_uploaded_file($_FILES["productImage"]["tmp_name"],$path);				  
					productResize($path, $path_thumb, $path_medium, $wishType);	
					
					$sql_updpro="UPDATE tbl_wishlist set 
								 product_image='".$path_medium."',
								 product_thumb='".$path_thumb."'								
								 where wishlist_id=$wishId";	
					$rst_updpro=mysql_query($sql_updpro) or die(mysql_error());
					if(mysql_affected_rows()>0) { 
						unlink($path);
						echo "<div id='suc-msg' align='center' style='width:650px;'> Product added with image to your wishlist. </div>"; 
					} else {
						echo "<div id='suc-msg' align='center' style='width:650px;'> Product added to your wishlist. </div>						
							<div id='err-msg' align='center' style='width:650px;'> Error in adding product image.</div>";
					}	
				}else {
					echo "<div id='suc-msg' align='center' style='width:650px;'> Product added to your wishlist. </div>						
						<div id='err-msg' align='center' style='width:650px;'> Error in adding product image. Upload jpg,png and gif formats only.</div>";
				}	
			 } else {
			 	echo "<div id='suc-msg' align='center' style='width:650px;'> Product added to your wishlist. </div>";
			 }		
		}
		else {
			echo "<div id='err-msg' align='center' style='width:650px;'> Error in adding wishlist item, try again. </div>";
		}
	}
		
		
	if(!empty($_POST['editWishListKey'])) {
		$wishListId=$_POST['editWishListKey'];
		
		//get wishlist old images
		$sqlGetImages = "SELECT product_image, product_thumb, wishlist_type FROM tbl_wishlist WHERE wishlist_id='$wishListId'";
		$rstGetImages = mysql_query($sqlGetImages) or die(mysql_error());
		$rowGetImages = mysql_fetch_array($rstGetImages);
		
		//check wishlist type for updating...
		
		if($rowGetImages["wishlist_type"]=='2') {
		
				$productName=$_POST['productName'];
				$wishprice=$_POST['wishprice'];
				$productDescription=$_POST['productDescription'];
				$sqlUpdWishListProd="UPDATE tbl_wishlist SET product_name='$productName', price='$wishprice', description='$productDescription' WHERE wishlist_id=$wishListId";
				$rstUpdWishListProd=mysql_query($sqlUpdWishListProd) or die(mysql_error());
				$noUpdWishListProd=mysql_affected_rows();
				if($noUpdWishListProd>0){ 			
					
					$wishId=$wishListId;
					
					$wishImage = $_FILES["productImage"]["name"];
					$imageInfo=pathinfo($wishImage);		
					$wishType = strtolower($imageInfo['extension']);
					if(!empty($wishType)) {						
						if($wishType=="jpeg" || $wishType=="jpg" || $wishType=="png" || $wishType=="gif") {				
							$rand=mt_rand();					
							$path="wishlist/source/".$rand.$wishId.".".$wishType;					
							$path_thumb="wishlist/thumb/".$rand.$wishId."_thumb.".$wishType;					
							$path_medium="wishlist/medium/".$rand.$wishId."_medium.".$wishType;									
							move_uploaded_file($_FILES["productImage"]["tmp_name"],$path);				  
							productResize($path, $path_thumb, $path_medium, $wishType);	
							
							$sql_updpro="UPDATE tbl_wishlist set 
										 product_image='".$path_medium."',
										 product_thumb='".$path_thumb."'								
										 where wishlist_id=$wishId";	
							$rst_updpro=mysql_query($sql_updpro) or die(mysql_error());
							if(mysql_affected_rows()>0) { 
								unlink($path);
								
								//delete old images
								if($rowGetImages[0]!="") { unlink($rowGetImages[0]); }
								if($rowGetImages[1]!="") { unlink($rowGetImages[1]); }
								
								echo "<div id='suc-msg'> Product updated with product image to your wishlist. </div>"; 
							} else {
								echo "<div id='suc-msg'> Wishlist product updated to your wishlist. </div>						
									<div id='err-msg'> Error in updating product image.</div>";
							}	
						}else {
							echo "<div id='suc-msg'> Wishlist product updated. </div>						
								<div id='err-msg'> Error in updating product image. Upload jpg,png and gif formats only.</div>";
						}	
					 } else {
						echo "<div id='suc-msg'> Wishlist product updated. </div>";
					 }		
				}
				else {
					echo "<div id='err-msg'> Error on updating wishlist product. </div>";
				}
		} // end of checking if wishlist type own
		 
		else {
				// otherwise wishlist should be added one....
				$wishprice=$_POST['wishprice'];				
				$sqlUpdWishListProd="UPDATE tbl_wishlist SET price='$wishprice' WHERE wishlist_id=$wishListId";
				$rstUpdWishListProd=mysql_query($sqlUpdWishListProd) or die(mysql_error());				
				if(mysql_affected_rows()>0){
					echo "<div id='suc-msg'> Wishlist price updated. </div>";
				} 
		
		}		
	}						   					   
				   
		 ?>
		 <form name="frmWishlist" id="frmWishlist" method="post" action="">
		 <input type="hidden" name="hidDelWishlist" id="hidDelWishlist">
         <input type="hidden" name="hidOrderWishlist" id="hidOrderWishlist">
         <input type="hidden" name="hidEditWishlist" id="hidEditWishlist">		 
		 <?php		   
				   $sqlSelLst="SELECT * FROM tbl_wishlist where user_id='$userId' ORDER BY product_order LIMIT 0,10";
				   //echo $sqlSelLst;
				   $rstSelLst=mysql_query($sqlSelLst);				  
				   $num=mysql_num_rows($rstSelLst);
				  
				  
						//$num=0;
				   if($num==0)
					{
					?>
					 <table width='100%' border='0' align='center' cellspacing='0'>
						<tr>
						  <td colspan="15" align="center">
						    <div class="box" style="float:none;">
							<div class="box_top">&nbsp;</div>
							<div class="box_middle">
							    <div class="boxtxt_out">&nbsp</div>
							    <div class="boxtxt_out"><em>Is there a product you have always wanted but couldn't find it for the right price?Let someone else find it for you!</em></div>
							    <div class="boxtxt_out">
							      <div class="box_img">
								      <img src="images/starthere_blue.png" style="cursor:pointer;" onclick="showAddWishlist(0,'divWishlistAction')" />
							      </div>
							    </div>
							</div>
							<div class="box_bottom">&nbsp;</div>
						    </div>
						  </td>
						</tr>
					    </table>
					<?
					}
				   else {
					 echo "<table width='100%' border='0' align='center' cellspacing='0'>";
				   echo "<tr class='head-tr'>
						  <td width='3%'>#</td>
						  <td width='50%'><strong>Product Name</strong></td>						  
						  <td width='20%' align='left'><strong>Wanted Price</strong></td>						 
						  <td width='20%'>&nbsp;</td>	
						</tr></table>";
						echo '<div style="max-height:160px; overflow-y:auto;width:`100%">';
						echo "<table width='100%' border='0' align='center' cellspacing='0'>";
				   		 $q=1; 		 
						 while($rowSelLst=mysql_fetch_array($rstSelLst))
					         {
						   $wishId=$rowSelLst['wishlist_id'];
						   $itemId = $rowSelLst['item_id'];
						   $wishlistType=$rowSelLst['wishlist_type'];
						   $productPrice = $rowSelLst['price'];						  
						   $productName=wordFormater($rowSelLst['product_name'],30);
						   
						   if($wishlistType=='2') {
						   		$proLink = "wishproduct=".$wishId;
						   } else {
						   		$proLink = "products=".$itemId;
						   }
						   							
						   echo "<tr>
						        <td height='28' width='3%'> $q </td>
								
								<td width='50%'><a href='index.php?$proLink' title='$productName'>$productName</a></td>								 
								 <td width='20%'>$".$productPrice." </td>								
<td width='20%'>";
if($rowSelLst['add_type']=="new") {
echo "<a href='javascript:void(0);' onClick='funEditWishlistForm($wishId,\"divWishlistAction\")'>Edit</a> | <a  href='javascript:void(0);' onClick='funDelWishlist($wishId)'>Delete </a>"; } else { echo "<a  href='javascript:void(0);' onClick='funDelWishlist($wishId)'>Delete </a>"; } echo "</td>
						       </tr>";							
						 
   			               echo "<tr>
									 <td colspan='4'> <div style='border-top:1px solid #CCCCCC;'> </div> </td>
								  </tr>";
						 $q++;
						 }
						 				    	
									
										
					echo "</table></div>";
					echo "<table width='100%' border='0' align='center' cellspacing='0'><tr><td colspan='4'>
		    <a href='javascript:void(0)' class='yellow_btn' onClick='showAddWishlist(0,\"divWishlistAction\")' style='float: left; height: 22px; margin: 10px 10px 0px 300px; 
		   padding: 7px 5px 0px; text-decoration: none;'>Add to List</a>
		   </td></tr></table>";
	}		
	?>
    <input type="hidden" name="hidWishlistType" id="hidWishlistType" value="2">		
	</form>
	<div id="divWishlistAction">  </div>  
  </td>
 </tr>
</table>
	</div>
<div class="fullbox_bot">&nbsp;</div>
</div><br /><br /><br />
<hr style="float:left; width:500px; display:none;" id="hr_line_panel_wishlist" />
</div>

</div>
<br />